
/*
 * Copyright (C) Yichun Zhang (agentzh)
 */


#ifndef DDDEFINE_H
#define DDDEFINE_H

#include <ngx_config.h>
#include <nginx.h>
#include <ngx_core.h>


#ifndef __model__
#define __model__ "lua"
#endif

#if defined(DDEBUG) && (DDEBUG)

#if (NGX_HAVE_VARIADIC_MACROS)

#define dd(...)\
    fprintf(stderr, "%s *** %s: ", __model__ ,__func__); \
    /*fprintf(stderr, "%s: %d, log: ", __FILE__, __LINE__);*/ \
    fprintf(stderr, __VA_ARGS__); \
    fprintf(stderr, "\n")

#else

#include <stdarg.h>
#include <stdio.h>
#include <stdarg.h>

static ngx_inline void
    dd(const char* fmt, ...) {
}

#endif

#if DDEBUG > 1

#define dd_enter() dd_enter_helper(r, __func__)

#if defined(nginx_version) && nginx_version >= 8011
#   define dd_main_req_count r->main->count
#else
#   define dd_main_req_count 0
#endif

static void dd_enter_helper(ngx_http_request_t *r, const char *func) {
    ngx_http_posted_request_t       *pr;

    fprintf(stderr, "dd enter %s %s %.*s %.*s?%.*s c:%d m:%p r:%p ar:%p pr:%p",
        __model__,
        func,
        (int)r->method_name.len, r->method_name.data,
        (int)r->uri.len, r->uri.data,
        (int)r->args.len, r->args.data,
        (int)dd_main_req_count, r->main,
        r, r->connection->data, r->parent);

    if (r->posted_requests) {
        fprintf(stderr, " posted:");

        for (pr = r->posted_requests; pr; pr = pr->next) {
            fprintf(stderr, "%p,", pr);
        }
    }
    fprintf(stderr, "\n");
}

#else

#define dd_enter()

#endif

#else

#if (NGX_HAVE_VARIADIC_MACROS)
#define dd(...)
#define dd_enter()

#else

#include <stdarg.h>

static ngx_inline void
    dd(const char* fmt, ...) {
}

static ngx_inline void
    dd_enter() {
}

#endif

#endif


#endif /* DDDEFINE_H */
